
$(function(){
    var validator = $("#queryForm").validate({
        errorElement: "title",
        ignoreTitle: true,
        submitHandler : function(form){
            var actionUrl = $(form).attr("action");
            if(actionUrl){
                return true;
            }else{
                queryFormSubmit();
            }

        },
        rules:{
            outHospitalStartDate: {
                required: true
            },
            outHospitalEndDate: {
                required: true
            }
        },

    });
});


/*点击查询按钮*/
function queryBtnClick(){
    $("input[name='currentPage']").val(1);
    $("#queryForm").attr("action",null).submit();
}

/*提交查询*/
function queryFormSubmit(){
    var submitData = $('#queryForm').serialize();
    var basePath = $("#basePath").val();
    layer.load(1);
    $("#export_h").html("");

    clearPage();
    $.ajax({
        url: basePath + "statistics_analysis/unplan_re_hospital",
        dataType: "json",
        data:submitData,
        success: function( data ) {
            var success = data['success'];
            if(success){
                var params = data['data'];

                var totalCounts = params['totalCounts'];
                if(totalCounts > 0){
                    layer.msg("查询成功");
                    var records = params['datas'];
                    addRows(records);
                    var pageSize = params['pageSize'];
                    var currentPage = params['currentPage'];

                    initPage(parseInt(totalCounts),parseInt(pageSize),parseInt(currentPage));
                }else{
                    layer.msg("查询结果为空");
                }

            }else{
                layer.msg("查询失败");
            }
            console.log(data);

        },
        error:function(XMLHttpRequest, textStatus, errorThrown){
            if(XMLHttpRequest.status == 200){
                if(textStatus == 'parsererror'){
                    layer.msg("登陆已过期，请重新登陆");
                }else{
                    layer.msg("未知错误，请联系管理员");
                }
            }else if(XMLHttpRequest.status == 0){
                if(textStatus == 'error'){
                    layer.msg("无法连接服务器，请检测网络");
                }else{
                    layer.msg("未知错误，请联系管理员");
                }
            }else{
                layer.msg("未知错误，请联系管理员");
            }
        },
        complete:function(XMLHttpRequest, textStatus){
            layer.closeAll('loading');
            parent.document.getElementById("mainContent").height=document.body.scrollHeight;
        }
    });

};

/*添加列表行*/
function addRows(records){
    var basePath = $("#basePath").val();
    for(var index in records){
        var record = records[index];
        var add_content=$("#template_tr").clone();
        add_content.removeAttr("hidden");
        if(record.diseases_count > 0){
            $("#export_h").html("<input type='button' value='导出' onclick='exportToExcel()'/>");
        }


        add_content.find("span[class='diseases_count']").html(record.diseases_count);
        add_content.find("span[class='rate_0']").html(record.rate_0);
        add_content.find("span[class='rate_15']").html(record.rate_15);
        add_content.find("span[class='rate_31']").html(record.rate_31);
        add_content.find("span[class='rate_more']").html(record.rate_more);
        add_content.find("span[class='avg_in_hospital_day']").html(record.avg_in_hospital_day);
        add_content.find("span[class='avg_in_hospital_cost']").html(record.avg_in_hospital_cost);
        $("#query_show_table tbody").append(add_content);
    }

}

/*清空列表*/
function clearPage(){
    $("#template_tr").siblings("tr").remove();
    $("#page_plus").html("");
    $("#totalPage").html("0");
    $("#currentPage").html("0/0");
}

/*初始化分页控件*/
function initPage(totalCounts,pageSize,currentPage){
    var visiblePages = 5;
    var totalPages =  totalCounts%pageSize==0?(totalCounts/pageSize):(parseInt(totalCounts/pageSize)+1);
    if(totalCounts < 1){
        $("#page_plus").html("");
        $("#totalPage").html("0");
        $("#currentPage").html("0/0");
    }else{
        $('#page_plus').jqPaginator({

            totalCounts: totalCounts,
            pageSize:pageSize,
            visiblePages: visiblePages,
            currentPage: currentPage,

            first:'<li class="first"><a href="javascript:void(0);">首页</a></li>',
            prev: '<li class="prev"><a href="javascript:void(0);">上一页</a></li>',
            next: '<li class="next"><a href="javascript:void(0);">下一页</a></li>',
            last: '<li class="last"><a href="javascript:void(0);">末页</a></li>',
            page: '<li class="page"><a href="javascript:void(0);">{{page}}</a></li>',
            onPageChange: function (num,type) {

                if(type !='init'){
                    $("input[name='currentPage']").val(num);
                    queryFormSubmit();
                }
                $("#totalPage").html(totalCounts);
                $("#currentPage").html(num+"/"+totalPages);
            }
        });
    }
}

function exportToExcel(){
    var basePath = $("#basePath").val();
    $("#queryForm").attr("action",basePath+"statistics_analysis/unplan_re_hospital_export_excel");
    $("#queryForm").submit();
}
